<template>
  <div class="esignCenter">
    <a-col :flex="1">
      <pro-form
        :loading="loading"
        ref="formRef"
        :form-json="formType"
        :form-data="formState"
        table
        :gutter="50"
        @search="handleSubmit"
        @resetForm="handleSubmit"
      ></pro-form>
    </a-col>
    <a-space style="margin-bottom: 10px"></a-space>
    <a-button
      style="margin-left: 10px"
      type="primary"
      @click="
        () => {
          visible = true;
        }
      "
    >
      <template #icon>
        <icon-plus />
      </template>
      添加签章
    </a-button>
    <a-row :loading="loading">
      <a-card :bordered="false" :loading="loading">
        <a-row :span="24" :gutter="20">
          <a-col :span="3.5" v-for="(item, index) in listData" :key="index">
            <a-card
              :style="{ width: '220px', borderRadius: '10px' }"
              style=""
              type="card-gutter"
            >
              <template #cover>
                <div style="height: 200px; width: 100%">
                  <a-image
                    fit="contain"
                    height="100%"
                    width="100%"
                    :src="item.seal_path"
                  />
                </div>
              </template>
              <a-card-meta :title="item.seal_name" description="">
              </a-card-meta>
              <div style="margin-top: 10px">
                <a-tag>
                  <template #icon>
                    <icon-tag />
                  </template>
                  {{ item.seal_code }}
                </a-tag>
                <a-tag
                  color="green"
                  v-if="item.seal_state === 1"
                  style="margin-top: 7px"
                  >启用</a-tag
                >
                <a-tag color="danger" v-if="item.seal_state === 2">禁用</a-tag>
                <a-tag
                  color="orange"
                  v-if="item.seal_type === 1"
                  style="margin-top: 7px; margin-left: 5px"
                  >印章</a-tag
                >
                <a-tag
                  color="arcoblue"
                  v-if="item.seal_type === 2"
                  style="margin-top: 7px; margin-left: 5px"
                  >签名</a-tag
                >
                <div
                  style="margin-top: 10px; display: flex; align-items: center;width: 100%;flex;justify-content: flex-end"
                >
                  <!--                  <span class="icon-hover"> <icon-edit /></span>-->
                  <a-popconfirm
                    style="display: inline"
                    @ok="handleDelete(item)"
                    position="left"
                    type="error"
                    content="删除后将不可恢复，真的要删除吗?"
                  >
                    <span class="icon-hover"><icon-delete /></span>
                  </a-popconfirm>
                </div>
              </div>
            </a-card>
          </a-col>
        </a-row>
      </a-card>
    </a-row>
    <Add v-if="visible" v-model:visible="visible" @refresh="restData" />
  </div>
</template>

<script lang="ts" setup>
  import useLoading from '@/hooks/loading';
  import { deleteSeal, seallist } from '@/api/seal';
  import Add from './add/index.vue';
  import { FormInstance, Message } from '@arco-design/web-vue';
  import { ref } from 'vue';
  const { loading, setLoading } = useLoading(false);
  const formRef = ref<FormInstance | null>(null);
  const visible = ref(false);

  const formType = ref([
    {
      name: 'seal_code',
      label: '印章编号',
      type: 'a-input',
      fieldProps: {
        span: 8,
      },
    },
    {
      name: 'seal_name',
      label: '印章名称',
      type: 'a-input',
      fieldProps: {
        span: 8,
      },
    },
    {
      label: '印章类型',
      name: 'seal_type',
      type: 'a-select',
      fieldProps: {
        span: 8,
      },
      selectOptions: [
        {
          value: 1,
          label: '印章',
        },
        {
          value: 2,
          label: '签名',
        },
      ],
    },
    {
      label: '印章状态',
      name: 'seal_state',
      slotName: 'status',
      type: 'a-select',
      fieldProps: {
        span: 8,
      },
      selectOptions: [
        {
          status: 'success',
          value: 1,
          label: '启用',
        },
        {
          status: 'danger',
          value: 2,
          label: '禁用',
        },
      ],
    },
  ]);
  const page_no = ref(1);

  const formState = ref();
  const listData = ref();
  function request() {
    loading.value = true;
    let formData = formRef.value.formModel;
    seallist({
      ...formData,
      page_no: page_no.value,
      page_size: 1000,
    })
      .then((res) => {
        listData.value = res.data.list;
        res.data.list.forEach((item: any) => {
          item.seal_path = import.meta.env.VITE_FILE_PATH + item.seal_path;
        });
      })
      .finally(() => {
        loading.value = false;
      });
  }
  function restData() {
    page_no.value = 1;
    request();
  }
  function handleDelete(item: any) {
    loading.value = true;
    deleteSeal({
      seal_ids: [item.seal_id],
    }).then((res) => {
      Message.success('删除成功！');
      request();
    });
  }

  onMounted(() => {
    request();
  });
  const handleSubmit = () => {
    request();
  };
</script>
<style scoped lang="less">
  .icon-hover {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    transition: all 0.1s;
  }
  .icon-hover:hover {
    background-color: rgb(var(--gray-2));
  }
  .esignCenter {
    padding: 20px;
  }

  .signature {
    background-color: #e9ecfc;
    border-radius: 5px;
  }
  .clear_a {
    cursor: pointer;
    position: absolute;
    right: 15px;
    bottom: 10px;
  }
</style>
